﻿
// RandomRollCallDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "RandomRollCall.h"
#include "RandomRollCallDlg.h"
#include"CChangePictureDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框


class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
public:
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)

END_MESSAGE_MAP()


// CRandomRollCallDlg 对话框



CRandomRollCallDlg::CRandomRollCallDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_RANDOMROLLCALL_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CRandomRollCallDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_BUTTON2, m_BtnRollCall);
	DDX_Control(pDX, IDC_EDIT1, m_TextShowName);
}



BEGIN_MESSAGE_MAP(CRandomRollCallDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_GETMINMAXINFO()
	ON_COMMAND(ID_32771, &CRandomRollCallDlg::On32771_EditCallList)
	ON_COMMAND(ID_32776, &CRandomRollCallDlg::On32776)
	ON_COMMAND(ID_32775, &CRandomRollCallDlg::On32775)
	ON_BN_CLICKED(IDC_BUTTON2, &CRandomRollCallDlg::OnBnClickedButton2)
	ON_COMMAND(ID_32774, &CRandomRollCallDlg::On32774)
	ON_WM_TIMER()
END_MESSAGE_MAP()


// CRandomRollCallDlg 消息处理程序

BOOL CRandomRollCallDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码

	m_TextFont.CreatePointFont(180,_T("黑体"));	//创建编辑框新字体
	m_TextShowName.SetFont(&m_TextFont);		//应用新字体

	this->MyLoadImage();	//加载背景图

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CRandomRollCallDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CRandomRollCallDlg::OnPaint()
{
	////绘制背景图片
	//CPaintDC myDc(this);
	//CImage image;
	//CRect myRect;
	//GetClientRect(&myRect);
	//image.Load(Manager::ProgramPath::Path() + _T("背景图片.jpeg"));
	////image.BitBlt(myDc, 0, 0, 50, 50, 0, 0);
	//::SetStretchBltMode(myDc.m_hDC,HALFTONE);
	//::SetBrushOrgEx(myDc.m_hDC, 0, 0, NULL);
	//image.Draw(myDc.m_hDC, myRect);
	//image.Destroy();

	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);

	}
	else
	{
		DrawBackGround();

		CDialogEx::OnPaint();
	}


}

void CRandomRollCallDlg::DrawBackGround()
{
	//绘制背景图片
	//CPaintDC myDc(this);
	//CImage image;
	//CRect myRect;
	//GetClientRect(&myRect);
	//image.Load(Manager::ProgramPath::Path() + _T("背景图片.jpeg"));
	////image.BitBlt(myDc, 0, 0, 50, 50, 0, 0);
	//::SetStretchBltMode(myDc.m_hDC, HALFTONE);
	//::SetBrushOrgEx(myDc.m_hDC, 0, 0, NULL);
	//image.Draw(myDc.m_hDC, myRect);
	//ReleaseDC(&myDc);
	//image.Destroy();

	if (m_Image.IsNull())
	{
		return;
	}

	CWnd* pWnd;
	pWnd = GetDlgItem(IDC_STATIC);
	CDC* pDC = pWnd->GetDC();
	HDC hDC = pDC->m_hDC;

	CRect rect_frame;
	//CImage m_Image;
	pWnd->GetClientRect(&rect_frame);
	//MyLoadImage();

	::SetStretchBltMode(hDC, HALFTONE);
	::SetBrushOrgEx(hDC, 0, 0, NULL);

	m_Image.Draw(hDC, rect_frame);
	ReleaseDC(pDC);
}

void CRandomRollCallDlg::MyLoadImage()
{
	if (!m_Image.IsNull())
	{
		m_Image.Destroy();
	}
	m_Image.Load(Manager::ProgramPath::Path() + PICBACKGROUND);
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CRandomRollCallDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CRandomRollCallDlg::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
	CPoint   pt(655, 520); //定义宽和高  
	lpMMI->ptMinTrackSize = pt; //限制最小宽和高  
	CDialog::OnGetMinMaxInfo(lpMMI);
}





void CRandomRollCallDlg::On32771_EditCallList()
{
	// TODO: 在此添加命令处理程序代码
	CEditRollCallListDlg dlg;
	INT_PTR response = dlg.DoModal();
}



void CRandomRollCallDlg::On32776()
{
	// TODO: 在此添加命令处理程序代码
	CAboutDlg dlg;
	dlg.DoModal();
}


void CRandomRollCallDlg::On32775()
{
	// TODO: 在此添加命令处理程序代码
	OnOK();
}


void CRandomRollCallDlg::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	//setBtnEnable(FALSE);
	////AfxBeginThread(threadShowText, this);
	//Manager::CallListManger callList;
	//callList.ReadFileByPath(Manager::ProgramPath::Path());
	//int r;

	//r = rand() % callList.list.GetSize();
	//setText(callList, r);
	//setBtnEnable(TRUE);

	
	//AfxBeginThread(threadShowText, this);
	Manager::CallListManger callList;
	callList.ReadFileByPath(Manager::ProgramPath::Path());
	if (callList.list.IsEmpty())
	{
		return;
	}
	setBtnEnable(FALSE);
	int r = rand() % callList.list.GetSize();
	Manager::CallListData jackport;
	jackport = callList.list[r];
	showNameStack.push(jackport);
	for (size_t i = 1; i < 5; i++)
	{
		r = rand() % callList.list.GetSize();
		showNameStack.push(callList.list[r]);
	}
	SetTimer(1, 200, nullptr);	//0.2秒执行一次
	setBtnEnable(FALSE);
}

void CRandomRollCallDlg::setBtnEnable(BOOL enable)
{
	UpdateData(TRUE);
	m_BtnRollCall.EnableWindow(enable);
	UpdateData(FALSE);

}

void CRandomRollCallDlg::setText(Manager::CallListManger& callList, int index)
{
	UpdateData(TRUE);
	CString str = callList.list[index].name + _T("\r\n") + callList.list[index].no_;
	m_TextShowName.SetWindowTextW(str);
	UpdateData(FALSE);
}



UINT threadShowText(LPVOID pParam)
{
	CRandomRollCallDlg* p = (CRandomRollCallDlg*)pParam;

	Manager::CallListManger callList;
	callList.ReadFileByPath(Manager::ProgramPath::Path());
	int r;
	for (size_t i = 0; i < 3; i++)
	{
		r = rand() % callList.list.GetSize();
		CString str = callList.list[r].name +_T('\n') + callList.list[r].no_;
		//p->setText(callList, r);
		Sleep(30);
	}
	p->setBtnEnable(TRUE);

	return 0;
}





void CRandomRollCallDlg::On32774()
{
	// TODO: 在此添加命令处理程序代码
	CChangePictureDlg dlg;
	if (dlg.DoModal() == IDOK)
	{
		m_Image.Destroy();
		CString gbPath = Manager::ProgramPath::Path() + PICBACKGROUND;
		CString midPath = Manager::ProgramPath::Path() + CChangePictureDlg::NAME_MIDSAVEPICNAME();
		//if (DeleteFile(Manager::ProgramPath::Path()+PICBACKGROUND))	//删除原背景
		//{
		//	m_Image.Load(midPath);	//加载中介
		//	m_Image.Save(gbPath);	//保存为背景
		//	m_Image.Destroy();
		//	DeleteFile(midPath);	//删除中介文件
		//}

		m_Image.Load(midPath);	//加载中介
		m_Image.Save(gbPath);	//保存为背景
		m_Image.Destroy();
		DeleteFile(midPath);	//删除中介文件
		MyLoadImage();
		DrawBackGround();
	}
}

CString CRandomRollCallDlg::NAME_PICBACKGROUND()
{
	return CString(PICBACKGROUND);
}


void CRandomRollCallDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (!showNameStack.empty())
	{
		auto data = showNameStack.top();
		UpdateData(TRUE);
		m_BtnRollCall.EnableWindow(FALSE);
		m_TextShowName.SetWindowTextW(data.name + _T("\r\n") + data.no_);
		UpdateData(FALSE);
		showNameStack.pop();
	}
	else
	{
		UpdateData(TRUE);
		m_BtnRollCall.EnableWindow(TRUE);
		UpdateData(FALSE);
		KillTimer(nIDEvent);
	}
	CDialogEx::OnTimer(nIDEvent);
}
